package linshuai;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class CAView extends Canvas
{
	private static final int PXSIZE = 3;

	public CAView() {
		super(CAModel.L*PXSIZE, CAModel.L*PXSIZE);
	}

	public void draw(CAModel model) {
		GraphicsContext gc = getGraphicsContext2D();
		gc.setFill(Color.WHITE);
		gc.fillRect(0, 0, getWidth(), getHeight());
		final Color c[] = new Color[]{Color.RED,Color.ORANGE,Color.GREEN};
		for(int y = 0; y < CAModel.L; y++)
			for(int x = 0; x < CAModel.L; x++)
				for(int t = 0; t < 3; t++)
					if(model.has(x,y,t)) {
						gc.setFill(c[t]);
						gc.fillRect(x*PXSIZE, y*PXSIZE, PXSIZE, PXSIZE);
						break;
					}
		gc.setFill(Color.gray(0.6));  // grid
		for(int x = 0; x <= CAModel.L; x++)
			gc.fillRect(x*PXSIZE, 0, 1, CAModel.L*PXSIZE);
		for(int y = 0; y <= CAModel.L; y++)
			gc.fillRect(0, y*PXSIZE, CAModel.L*PXSIZE, 1);
	}
}
